<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="company" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDep('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-tree :data="list" default-expand-all>
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon :icon-class="scope.node.expanded ? 'shouqi' : 'zhankai'" />
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDep(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDep(scope.data.id, scope.data.pid)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDep(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 对话框 -->
    <el-dialog
      v-if="showDialog"
      :visible.sync="showDialog"
      :title="title"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <depDialog
        :pid="id"
        :is-edit="isEdit"
        :origin-list="originList"
        :ppid="pid"
        @success="success"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arrayToTree } from '@/utils/index'
import depDialog from './depDialog.vue'
export default {
  components: {
    depDialog
  },
  data() {
    return {
      // 对话框显示
      showDialog: false,
      // 依赖一份树形数据
      list: [],
      // 被点击的部门的id
      id: '',
      // 被点击的部门的pid
      pid: '',
      // 对话框title
      title: '',
      // 是否是编辑布尔值
      isEdit: false,
      // 子组件表单校验数据数组
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  // methods:事件处理函数
  methods: {
    // 加载组织架构
    async loadDepartments() {
      const res = await getDepartments()
      // 删除第一个返回的数据第一个元素
      res.data.data.depts.shift()
      // 将数据绑定到data中
      this.list = arrayToTree(res.data.data.depts, '')
      this.originList = res.data.data.depts.map(item => {
        return {
          name: item.name,
          code: item.code,
          id: item.id,
          pid: item.pid
        }
      })
    },
    // 添加子部门
    addDep(id) {
      this.showDialog = true
      this.title = '新增部门'
      // 不是编辑
      this.isEdit = false
      this.id = id
    },
    // 添加新部门成功关闭弹窗并刷新列表
    success() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 删除部门
    delDep(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartment(id)
        this.loadDepartments()
      }).catch(() => {})
    },
    // 编辑部门
    editDep(id, pid) {
      this.showDialog = true
      this.title = '编辑部门'
      // 是编辑
      this.isEdit = true
      this.id = id
      this.pid = pid
    }
  }
}
</script>
